<template>
	<view class="facilityOrder-container">
		<view class="dateTimePicker">
			<view
				@click="selectDateTime(1)"
				:class="dateTimeClass1">
				今天{{dateTime.today | formateDate2}}
			</view>
			<view
				@click="selectDateTime(2)"
				:class="dateTimeClass2">
				明天{{dateTime.tomorrow | formateDate2}}
			</view>
			<view
				@click="selectDateTime(3)"
				:class="dateTimeClass3">
				后天{{dateTime.afterTomorrow | formateDate2}}
			</view>
		</view>
		<view 
			class="facilityList" 
			:style="{height:cssheight}">
			<scroll-view 
				scroll-y="true" 
				class="scroll-Y"
				lower-threshold="50"
				@scrolltolower="scrollToLower">
				<view 
					class="facilityItem" 
					v-for="(item,index) in list" 
					:key="item.id"
					@click="goFacilityItemDetail(item.id,item.state)">
					<view class="title">{{item.facilityName}}</view>
					<view class="contentBox">
						<view class="left">
							<view class="content">
								<text>
									{{item.facilityDescribe}}
								</text>
							</view>
						</view>
						<view class="right">
							<view class="statusOK" v-if="item.state === 1">
								点击预约
							</view>
							<view class="statusNo" v-if="item.state === 0">
								暂不可用
							</view>
						</view>
					</view>
				</view>
				<view class="noData" v-if="noDataShow">
					--没有更多了，我也是有底线的--
				</view>
			</scroll-view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				current: 1,
				limit: 8,
				list: [],
				total: 0,
				pages: 0,
				dateTime:{
					today:null,
					tomorrow:null,
					afterTomorrow:null,
				},
				nowPickerDate: null,
				dateTimeClass1:'active',
				dateTimeClass2:'',
				dateTimeClass3:'',
				cssheight: '300rpx',
				noDataShow: false
			};
		},
		onReady() {
			var _this = this
			var css = ''
			uni.getSystemInfo({
				success: (resu) => {
					const query = uni.createSelectorQuery()
					query.select('.facilityList').boundingClientRect()
					query.exec(function(res){
						_this.height = resu.windowHeight -res[0].top + 'px'
						// console.log(_this.height)
						css = _this.height
					})
				}
			})
			this.cssheight = css
		},
		onLoad() {
			this.getDateTime()
			this.getFacilityList(1,this.dateTime.today)
		},
		methods:{
			goFacilityItemDetail(id,state){
				const d = this.nowPickerDate
				const y = d.getFullYear().toString().padStart(2,0)
				const m = (d.getMonth()+1).toString().padStart(2,0)
				const day = d.getDate().toString().padStart(2,0)
				var reqDate = y+"-"+m+"-"+day
				if(state == 0){
					uni.showToast({
						title:'场地不可用!',
						image:'../../static/Icon/facility/notAvailable1.png'
					})
				}else{
					uni.navigateTo({
						// url:'./facilityDetail/facilityDetail?id='+id+'&date='+reqDate,
						url:'./facilityDetail/facilityDetail?id='+id+'&date='+reqDate,
						fail: (res) => {
							console.log(res)
						}
					})
				}	
			},
			scrollToLower(){
				// console.log('触底')
				var page = this.current + 1
				this.getFacilityList(page,this.nowPickerDate)
			},
			getFacilityList(page,date){
				const d = date
				const y = d.getFullYear().toString().padStart(2,0)
				const m = (d.getMonth()+1).toString().padStart(2,0)
				const day = d.getDate().toString().padStart(2,0)
				var reqDate = y+"-"+m+"-"+day
				
				this.current = page
				
				this.$myReq({
					url:`/owner/facilityOrder/getFacilityList/${this.current}/${this.limit}/${reqDate}`,
					method: 'POST'
				})
				.then((res) => {
					// console.log(res)
					this.list = [...this.list,...res.data.records]
					this.total = res.data.total
					this.pages = res.data.pages
					this.current = res.data.current
					if(res.data.records.length < 8){
						this.noDataShow = true
					}
				})
				.catch((err) => {
					console.log(err)
				})
			},
			selectDateTime(index){
				if(index == 1){
					this.dateTimeClass2 = ''
					this.dateTimeClass3 = ''
					this.dateTimeClass1 = 'active'
					this.nowPickerDate = this.dateTime.today
					this.list = []
					this.getFacilityList(1,this.dateTime.today)
				}else if(index == 2){
					this.dateTimeClass1 = ''
					this.dateTimeClass3 = ''
					this.dateTimeClass2 = 'active'
					this.nowPickerDate = this.dateTime.tomorrow
					this.list = []
					this.getFacilityList(1,this.dateTime.tomorrow)
				}else if(index == 3){
					this.dateTimeClass1 = ''
					this.dateTimeClass2 = ''
					this.dateTimeClass3 = 'active'
					this.nowPickerDate = this.dateTime.afterTomorrow
					this.list = []
					this.getFacilityList(1,this.dateTime.afterTomorrow)
				}
			},
			getDateTime(){
				this.dateTime.today = new Date()
				this.nowPickerDate = new Date()
				var tomorrow = new Date()
				tomorrow.setDate(tomorrow.getDate() + 1)
				this.dateTime.tomorrow = tomorrow
				var afterTomorrow = new Date()
				afterTomorrow.setDate(afterTomorrow.getDate() + 2)
				this.dateTime.afterTomorrow = afterTomorrow
			}
		}
	}
</script>

<style lang="scss">
	 .facilityOrder-container{
		.dateTimePicker{
			padding: 40rpx 40rpx 0rpx 40rpx;
			display: flex;
			flex-wrap: wrap;
			justify-content: space-between;
			view{
				text-align: center;
				color: $main-garyB;
				padding: 15rpx 30rpx 15rpx 30rpx;
				// border: 1px solid;
				width: 100rpx;
				font-size: 30rpx;
				margin-left: 10rpx;
				margin-right: 10rpx;
			}
		}
	 }
	  .facilityList{
			padding: 0rpx 20rpx 0rpx 20rpx;
			// border: 1px solid blue;
			background-color: #F1F2F3;
			scroll-view{
				height: 100%;
				.noData{
					font-size: 30rpx;
					text-align: center;
					padding: 20rpx 0rpx;
				}
				.facilityItem{
					margin-bottom: 20rpx;
					background-color: #FFFFFF;
					margin-top: 30rpx;
					padding: 30rpx 30rpx 30rpx 30rpx;
					border-radius: 20rpx;
					.title{
						font-size: 35rpx;
						margin-bottom: 30rpx;
						font-weight: bold;
					}
					.contentBox{
						display: flex;
						flex-wrap: wrap;
						justify-content: space-between;
						.left{
							width: 450rpx;
							.content{
								color: $main-garyB;
								margin-bottom: 25rpx;
								text{
									font-size: 30rpx;
									display: -webkit-box;
									overflow: hidden;
									text-overflow: ellipsis;
									word-wrap: break-word;
									white-space: normal !important;
									-webkit-line-clamp: 2;
									-webkit-box-orient: vertical;
								}
							}
							// .timeTable{
							// 	font-size: 25rpx;
							// 	color: $main-colorB;
							// }
						}
						.right{
							width: 200rpx;
							display: flex;
							align-items: center;
							.statusOK{
								width: 90%;
								height: 80rpx;
								margin: 0 auto;
								text-align: center;
								line-height: 80rpx;
								border-radius: 10rpx;
								color: white;
								background-color: #409EFF;
								font-size: 30rpx;
							}
							.statusNo{
								width: 90%;
								height: 80rpx;
								margin: 0 auto;
								text-align: center;
								line-height: 80rpx;
								border-radius: 10rpx;
								color: white;
								background-color: #F56C6C;
								font-size: 30rpx;
							}
						}
					}
				}
			}
		}
		.active{
		 color: black;
		 border-bottom: 2px solid $main-colorA;
	 }
</style>
